package com.ng.desktop.expansionsa.view.alquiler;

import java.awt.Window;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.swing.ImageIcon;

import com.ng.desktop.expansionsa.model.Alquiler;
import com.ng.desktop.expansionsa.persistence.AlquilerPersistence;
import com.ng.desktop.expansionsa.view.alquiler.FilterNotificacion.Opcion;
import com.ng.framework.view.desktop.NGGUIUtils;
import com.ng.framework.view.desktop.NGTablePanel;

public class AlquilerTablePanel extends
		NGTablePanel<Alquiler, AlquilerPersistence> {
	
	private static final long serialVersionUID = 7677587381889272905L;

	private static final String[] COLUMN_NAMES = new String[] { "Id",
		"Cliente", "Fecha", "Dirección de la Obra", "Estado", "Alerta" };

	private static final int[] COLUMN_WIDTHS = { 50, 240, 100, 350, 140, 100 };
	
	private static final DateFormat FORMATO_FECHA = new SimpleDateFormat("dd/MM/yyyy");
	
	private static final ImageIcon ICONO_CON_NOTIFICACION = NGGUIUtils.getImageIcon("icons/exclamation-red.png");
	
	private static final ImageIcon ICONO_SIN_NOTIFICACION = NGGUIUtils.getImageIcon("icons/tick.png");
	
	private FilterNotificacion filterNotificacion;
	
	public AlquilerTablePanel(AlquilerPersistence persistence) {
		this(persistence, null);
	}

	public AlquilerTablePanel(AlquilerPersistence persistence, FilterNotificacion filterNotificacion) {
		super(persistence);
		
		this.filterNotificacion = filterNotificacion;
	}

	@Override
	protected Object[] toRow(Alquiler dataObject) {
		Object[] row = null;
		
		if(agregarFila(dataObject)) {
			row = new Object[] { dataObject.getId(), dataObject.getCliente().toString(), FORMATO_FECHA.format(dataObject.getFecha()), dataObject.getDireccionObra(), dataObject.getEstadoAlquiler().toString(), dataObject.getAlerta() ? ICONO_CON_NOTIFICACION : ICONO_SIN_NOTIFICACION };			
		}
		
		return row;
	}
	
	private boolean agregarFila(Alquiler alquiler) {
		boolean agregar = true;
		
		if(filterNotificacion != null && filterNotificacion.getOpcionSeleccionada() == Opcion.NO) {
			agregar = !alquiler.getAlerta();
		}
		
		return agregar;
	}
	
	@Override
	protected String[] getColumnNames() {
		return COLUMN_NAMES;
	}

	@Override
	protected int[] getColumnWidths() {
		return COLUMN_WIDTHS;
	}

	@Override
	protected boolean edit(AlquilerPersistence persistence, Alquiler instance) {
		Window owner = NGGUIUtils.findWindow(this);
		
		AlquilerEditDialog dialog = new AlquilerEditDialog(owner, persistence, instance);
		dialog.setLocationRelativeTo(owner);
		dialog.setVisible(true);
		
		return true;
	}

	@Override
	protected Alquiler getSelectedInstance(AlquilerPersistence persistence,
			Object[] rowData) {
		return persistence.findById((Long) rowData[0]);
	}

	@Override
	protected boolean create(AlquilerPersistence persistence) {
		Window owner = NGGUIUtils.findWindow(this);
		
		AlquilerEditDialog dialog = new AlquilerEditDialog(owner, persistence);
		dialog.setLocationRelativeTo(owner);
		dialog.setVisible(true);
		
		return true;
	}

}
